package com.delio.client.reportes;

import java.util.Date;
import java.util.HashMap;

import com.delio.client.idioma.DelioConstants;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Label;
import com.extjs.gxt.ui.client.widget.Status;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.Validator;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.RootPanel;


public class ClienteReporteForm extends ContentPanel{
	DateField desde;
	DateField hasta;
	Button buscar;
	Status status;
	Button print;
	ContentPanel hidePanel;
	private DelioConstants constants = GWT.create(DelioConstants.class);
	
	
	public ClienteReporteForm(){
      
	  setLayout(new FitLayout());
      setFrame(false);		
	  setIcon(IconHelper.createPath("images/icons/table.png"));  
	  setHeading("Reporte de Clientes");
	  

      desde = new DateField();        	
      desde.getPropertyEditor().setFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
      desde.setAllowBlank(false);
      desde.setMaxValue(new Date());
      
      hasta = new DateField();        	
      hasta.getPropertyEditor().setFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
      hasta.setAllowBlank(false);
      hasta.setValue(new Date());
      hasta.setMaxValue(new Date());
	  
      hasta.setValidator(new Validator() {			
			@Override
			public String validate(Field<?> field, String value) {
				if(desde.getValue() !=null && hasta.getValue() != null){
					if(desde.getValue().compareTo(hasta.getValue())>0)
						return "Debe ser mayor";
					return null;
				}
				return null;
			}
	  });
         
      
      final ClienteReporteForm crf = this;

      ToolBar toolBar = new ToolBar();
      
      toolBar.add(new Label("desde"));
      toolBar.add(desde);
              
      Label hastaL = new Label("Hasta");
      hastaL.setStyleAttribute("paddingLeft", "20px");
      toolBar.add(hastaL);
      toolBar.add(hasta);
      
      status = new Status();
      status.setBusy("Cargando");
      status.hide();    
      status.setAutoWidth(true);
      status.setStyleAttribute("paddingLeft", "20px");
      toolBar.add(status);
      
       buscar = new Button("Buscar", IconHelper.createPath("images/icons/find.png"));
       buscar.setStyleAttribute("paddingLeft", "20px");
      
    
      
      SelectionListener<ButtonEvent> sl_find;        
      sl_find = new SelectionListener<ButtonEvent>(){
       	@Override
		public void componentSelected(ButtonEvent ce) {		
			if(validate()){
			 status.show();
			 buscar.hide();
			 crf.removeAll();
			 HashMap<String, Date> param = new HashMap<String, Date>();
			 param.put("desde", desde.getValue());
			 param.put("hasta", hasta.getValue());				
			 System.out.println("entro en ClienteReporteForm*******************");
			 PdfReportViewer reportViewer = new PdfReportViewer("reports/prueba", param, status,buscar, print);
				
			 crf.add(reportViewer);
			 crf.layout();
			}
		}
      };
      buscar.addSelectionListener(sl_find);
      
      toolBar.add(buscar);
      
      print = new Button("Imprimir", IconHelper.createPath("images/icons/print.png"));
      print.setStyleAttribute("paddingLeft", "20px");
      print.setEnabled(false);
              
      toolBar.add(print);
		
      setTopComponent(toolBar);
      
      if(hidePanel!= null)
        hidePanel.removeFromParent();
        hidePanel= new ContentPanel();        
        hidePanel.setVisible(false);        
        RootPanel.get().add(hidePanel); 
  }
	
	private boolean validate() {
	  boolean des = desde.isValid();
	  boolean has = hasta.isValid();
		
	  return des & has;
	}
	
	
}